<?php

namespace Admin;

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */

/**
 * Description of TonesController
 *
 * @author dung
 */
use AdminController;
use Config;
use Exception;
use Input;
use Lang;
use Redirect;
use Sentry;
use Str;
use Tone;
use Validator;
use View;

class TonesController extends AdminController
{

    /**
     * Show a list of all the tones.
     *
     * @return View
     */
    public function getIndex()
    {
        // get all tones

        $tones = Tone::get();

        return View::make('backend/tones/index', compact('tones'));
    }

    /*
     * create tone form
     * @return View
     */

    public function getCreate()
    {
        if (!Sentry::getUser()->hasAnyAccess(['tone', 'tone.create']))
            return View::make('backend/notallow');
        $data['tone'] = new Tone();
        return View::make('backend/tones/edit', compact('data'));
    }

    /**
     * processing create tone
     * @return Redirect
     */
    public function postCreate()
    {
        if (!Sentry::getUser()->hasAnyAccess(['tone', 'tone.create']))
            return View::make('backend/notallow');
        $data = Input::get('data');
        $rules = array('title' => "required");
        $validation = Validator::make($data['Tone'], $rules);
        if ($validation->fails())
            return Redirect::back()->withInput()->withErrors($validation);
        $data['Tone']['slug'] = Str::slug($data['Tone']['title']);
        $tone = new Tone();
        $id = $tone->saveItem($data['Tone']);
        return Redirect::route('update/tone', $id);
    }

    /*
     * show edit tone form
     * @return View
     */

    public function getEdit($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['tone', 'tone.edit']))
            return View::make('backend/notallow');
        if (is_null($tone = Tone::find($id))) {
            // Redirect to the tones management page
            return Redirect::route('tones')->with('error', '');
        }
        $data['tone'] = $tone;
        Return View::make('backend/tones/edit', compact('data'));
    }

    /**
     *
     */

    public function getDelete($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['tone', 'tone.delete']))
            return View::make('backend/notallow');
        if (is_null($tone = Tone::find($id)))
            return Redirect::to('admin/tones')->with('error', '');
        try {
            $tone->delete();
            return Redirect::to('admin/tones');
        } catch (Exception $ex) {
            return Redirect::to('delete/tones', $id)->with('error', $ex);
        }
    }

    /**
     *
     */
    public function getRestore($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['tone', 'tone.delete']))
            return View::make('backend/notallow');
        if (is_null($tone = Tone::withTrashed()->find($id)))
            return Redirect::to('admin/tones')->with('error', '');
        $tone->restore();
        return Redirect::to('admin/tones');
    }

}
